package pattern;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: bjc
 * Date: 2025-08-24
 * Time: 21:53
 */
class SingletonLazy2 {
    private volatile static SingletonLazy2 instance = null;

    private SingletonLazy2() {
    }

    public static SingletonLazy2 getInstance() {
        //如果instance为null，说明是首次嗲用，首次调用需要考虑线程安全问题，就要加锁
        //如果非null，说明已经创建了实例，不需要加锁
        if (instance == null) { //判断是否需要加锁
            synchronized (SingletonLazy2.class) {
                if (instance == null) { //判断是否需要创建实例，确保只创建一个实例
                    instance = new SingletonLazy2();
                }
            }
        }
        return instance;
    }
}

public class Test4 {
}
